<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - 系统管理</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background-color: #f5f7fa;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .login-container {
            max-width: 400px;
            width: 100%;
            background-color: white;
            border-radius: 12px;
            box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
            padding: 30px;
        }
        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }
        .login-header h2 {
            color: #2c3e50;
            font-weight: 600;
        }
        .form-label {
            font-weight: 500;
            color: #34495e;
        }
        .form-control {
            border-radius: 8px;
            border: 1px solid #ddd;
            transition: border-color 0.3s ease, box-shadow 0.3s ease;
        }
        .form-control:focus {
            border-color: #4a6fa5;
            box-shadow: 0 0 0 2px rgba(74, 111, 165, 0.2);
            outline: none;
        }
        .btn-primary {
            background-color: #4a6fa5;
            border-color: #4a6fa5;
            border-radius: 8px;
            padding: 12px;
            font-weight: 600;
            transition: background-color 0.3s ease;
        }
        .btn-primary:hover {
            background-color: #3d5b8c;
            border-color: #3d5b8c;
        }
        .alert {
            border-radius: 8px;
            margin-top: 20px;
        }
        .input-icon {
            position: relative;
        }
        .input-icon i {
            position: absolute;
            left: 12px;
            top: 50%;
            transform: translateY(-50%);
            color: #95a5a6;
        }
        .input-icon .form-control {
            padding-left: 36px;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="login-header">
            <h2>系统登录</h2>
            <p class="text-muted">请输入您的账号和密码</p>
        </div>
        
        <form id="loginForm">
            <div class="mb-4">
                <label for="username" class="form-label">用户名</label>
                <div class="input-icon">
                    <i class="fas fa-user"></i>
                    <input type="text" class="form-control" id="username" placeholder="请输入用户名" required>
                </div>
            </div>
            
            <div class="mb-4">
                <label for="password" class="form-label">密码</label>
                <div class="input-icon">
                    <i class="fas fa-lock"></i>
                    <input type="password" class="form-control" id="password" placeholder="请输入密码" required>
                </div>
            </div>
            
            <button type="submit" class="btn btn-primary w-100">登录</button>
        </form>
        
        <div id="errorMessage" class="alert alert-danger" style="display: none;">
            <!-- 错误信息将在这里显示 -->
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://kit.fontawesome.com/a076d05399.js" crossorigin="anonymous"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const loginForm = document.getElementById('loginForm');
            const errorMessage = document.getElementById('errorMessage');
            
            loginForm.addEventListener('submit', async function(event) {
                event.preventDefault();
                
                const username = document.getElementById('username').value;
                const password = document.getElementById('password').value;
                
                // 简单的前端验证
                if (!username || !password) {
                    showError('请输入用户名和密码');
                    return;
                }
                
                try {
                    // 生成JWT格式的token
                    const token = generateJWT(username);
                    
                    // 将token存储到localStorage
                    localStorage.setItem('token', token);
                    
                    // 解码并显示token信息便于调试
                    try {
                        const parts = token.split('.');
                        if (parts.length === 3) {
                            const decodedPayload = JSON.parse(decodeURIComponent(escape(atob(parts[1]))));
                            console.log('登录成功，生成的token payload内容:', decodedPayload);
                        }
                    } catch (e) {
                        console.log('token解码失败:', e.message);
                    }
                    
                    // 跳转到提现页面，同时将token作为URL参数传递
                    window.location.href = `/withdrawal?token=${encodeURIComponent(token)}`;
                    
                } catch (error) {
                    showError('登录失败，请重试');
                    console.error('登录错误:', error);
                }
            });
            
            // 生成符合JWT格式的token
            function generateJWT(username) {
                // 生成符合JWT格式的测试token，包含正确的payload信息
                const header = btoa(JSON.stringify({"alg":"HS256","typ":"JWT"}));
                
                // 创建包含所有必要字段的payload
                const payload = JSON.stringify({
                    "ID": 1,
                    "NickName": username,
                    "AuthorityId": 1,
                    "exp": Math.floor(Date.now() / 1000) + 3600, // 1小时后过期
                    "iat": Math.floor(Date.now() / 1000), // 签发时间
                    "username": username
                });
                
                // Base64编码payload（注意处理UTF-8编码问题）
                const encodedPayload = btoa(unescape(encodeURIComponent(payload)));
                const signature = "mock_signature_for_development"; // 模拟签名
                
                return `${header}.${encodedPayload}.${signature}`;
            }
            
            // 显示错误信息
            function showError(message) {
                errorMessage.textContent = message;
                errorMessage.style.display = 'block';
                setTimeout(() => {
                    errorMessage.style.display = 'none';
                }, 3000);
            }
        });
    </script>
</body>
</html>